import sys
import numpy as np
from matplotlib import font_manager, pyplot as plt


class Pic:
    def zhe(self, x_name, y_name, x_data, datas, rangeStart, rangeEnd, saveName=None):
        zhfont1 = font_manager.FontProperties(fname="pyScripts/SourceHanSansSC-Regular.otf")
        fig, ax = plt.subplots()
        # 去边框
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        # ax.spines['left'].set_visible(False)
        # ax.spines['bottom'].set_visible(False)
        # plt.title(title, fontproperties=zhfont1, fontsize=16)
        # 刻度范围
        plt.ylim((rangeStart, rangeEnd))
        # 坐标轴名称, loc="right"
        plt.xlabel(x_name, fontproperties=zhfont1, fontsize=15)
        # loc="top"
        plt.ylabel(y_name, fontproperties=zhfont1, fontsize=15)
        x = np.array(x_data)
        handles = []
        labels = []
        for i in datas:
            tmp = []
            for d in i[0:-1]:
                tmp.append(float(d))
            data = np.array(tmp)
            # 折线格式
            handle, = plt.plot(x, data, "o-")
            handles.append(handle)
            labels.append(i[-1])
        plt.rcParams['font.sans-serif'] = ['SimHei']  ###解决legend中文乱码
        plt.rcParams['axes.unicode_minus'] = False
        # 图注
        plt.legend(handles=handles, labels=labels, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)
        # 网格线
        plt.grid(axis='y')
        # 解决图注放在图外区域，显示不全的问题
        fig.subplots_adjust(right=0.8)
        if saveName:
            # 保存为图片
            plt.savefig('/home/ruoyi/uploadPath/download/{}'.format(saveName))
        # 实时展示
        # plt.show()

    def zhu(self):
        pass


if __name__ == '__main__':
    # 非windows系统需要把SimHei.ttf文件放在your_path/site-packages/matplotlib/mpl-data/fonts/ttf目录下
    # 然后清除下面打印的文件
    # import matplotlib
    # print(matplotlib.get_cachedir())
    try:
        datas = sys.argv[1:]
        dataNums = len(datas) - 5
        dataList = []
        start = int(datas[4].split("=")[0])
        end = int(datas[4].split("=")[1])
        for j in datas[6:]:
            dataList.append(j.split("-"))
        if datas[0] == "zhe":
            # Pic().zhe(datas[1], datas[2], datas[3].split("_"), dataList, start, end)
            Pic().zhe(datas[1], datas[2], datas[3].split("_"), dataList, start, end, datas[5])
        elif datas[0] == "zhu":
            pass
        else:
            print("失败：图表类型参数校验不通过！")
    except Exception as e:
        print("失败：", e)
